package com.fy56.platform.goodsTracking.web;

import com.fy56.platform.goodsTracking.commons.unit.SystemConstants;
import com.fy56.platform.goodsTracking.model.GoodsPublic;
import com.fy56.platform.goodsTracking.model.GoodstrackedRecordTopPublicF6;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 上传货物跟踪本地队列消费者
 */
public class UploadTracePoll implements Runnable {
	private static final Logger logger = Logger.getLogger(UploadTracePoll.class);

	@Override
	public void run() {
		while (!CommonLocalLinkedQueue.getQueue().isEmpty()) {

			Map<String, Object> map = CommonLocalLinkedQueue.getQueue().poll();
			Long begin = System.currentTimeMillis();
			try {
				if(map.get("goodsTop") != null){
				    try{
                        List<GoodstrackedRecordTopPublicF6> subs = (List<GoodstrackedRecordTopPublicF6>) map.get("goodsTop");
                        if(subs!=null){
                            for (GoodstrackedRecordTopPublicF6 sub : subs) {
                                TraceUtil.doPostTop(sub);
                            }
                        }
                    }catch (Exception e){

                    }
				}else if(map.get("goodsList") != null){
				    try{
                        List<GoodsPublic> goods = (List<GoodsPublic>) map.get("goodsList");
					    if(goods!=null){
						    for (GoodsPublic good:goods){
                            TraceUtil.doPost(good);
						    }
					    }
                    }catch (Exception e){

                    }

				}
				Thread.sleep(100);
			} catch (Exception e) {
				logger.error("上传货物跟踪本地队列消费，post请求异常", e);
			}
			Long end = System.currentTimeMillis();
			logger.info("用时:" + (end - begin) + "毫秒。");
		}
		CommonLocalLinkedQueue.getLatch().countDown();
	}

}
